<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<view v-show="api_host=='https://www.x-mii.com/app/index.php'">
			<ad unit-id="adunit-ec96c4c3f5bef460" ></ad>			
		</view>
		<!-- #endif -->

		<component-diy
		:diy_id="diy_id"
		:diy_data="diy"
		></component-diy>
		<view v-if='!diy_id' >
		
			<swiper class="banner" v-bind:style="{'height':bnrHeight+'rpx'}" :indicator-dots='true' :autoplay='true' :interval='5000'
			 :duration='300' circular='true'>
				<swiper-item v-for="(item, index) in bnrUrl">
					<image @tap="gotoUrl(item.link)" v-bind:src="item.image" mode='widthFix'></image>
				</swiper-item>
			</swiper>
		
		
			<view class="category" v-show="category_show">
				<view class="list">
					<!--
					<navigator open-type="switchTab" 
					v-bind:url="'/sm_shop/pages/category/category?category_id='+ item.id" 
					class="item" 
					v-for="(item,index) in category_list">
						<image v-bind:src="item.image"></image>
						<view>{{item.name}}</view>
					</navigator>
					-->
					<view open-type="switchTab"
					@click="gotoCategoryTab( item )"
					class="item" 
					v-for="(item,index) in category_list">
						<image v-bind:src="item.image"></image>
						<view>{{item.name}}</view>
					</view>
				</view>
			</view>
		
			<view class="tuijian">
				<!-- <view class="title">
					<image mode="widthFix" src="/static/image/skin/tuijian-title.jpg"></image>
				</view> -->
				<view class="product-list">
		
					<block v-for="(item, index ) in recommend.goods_list">
						<view v-bind:id="'goods-'+item.id" class="item">
		
							<navigator class="img" v-bind:url="'/sm_shop/pages/product/product?product_id='+item.id">
								<image mode="widthFix" v-bind:src="item.image" alt="" />
							</navigator>
							<navigator>
								<text class="name">{{item.name}}</text>
							</navigator>
		
							<view class="sale-number">
								<text v-if="item.special>0" class="old-price text-line-through">¥{{item.price}}</text>
								销量:{{item.sale}}
							</view>
							<view class="price-buy">
								<view class="price">
									<text v-show="item.special<=0" class="text">¥{{item.price}}</text>
									<text v-show="item.special>0" class="text">¥{{item.special}}</text>
								</view>
								<view class="buy" @tap="gotoBuy( item )">
									<text class="text">马上抢</text>
								</view>
		
							</view>
						</view>
					</block>
		
				</view>
			</view>
		
		</view>
		<trademark></trademark>
		
	</view>
	
</template>

<script>
	const app = getApp();
	import component_diy from '../../common/diy/diy';
	import trademark from '../../common/trademark';
	export default {
		data: function() {
			return {
				// app_id:app.globalData.app_id,
				api_host:app.globalData.api_host,
				title: '首页',
				bnrUrl: [],
				bnrWidth: '100%',
				bnrHeight: '370',
				diy_id:0,
				diy:[],
				recommend: {

					goods_list: [],
					key: '',
					title: ''
				},
				category_list: [],
				category_show: 0,

				float_nav_show: 0,
			}
		},
		onLoad( option ) {
			
			// todo 查看是否扫用户推荐码进入,并存储到本地
			if(option.scene){
				const scene = decodeURIComponent(option.scene);
				var reg = /referee=(\d+)/;
				var res = reg.exec( scene );
				if( res ){
					uni.setStorage({
						key:'referee',
						data: res[1]
					});
				}
			}


			// todo referee 参数存入本地存储， 新用户注册时带上
			
			
			this.init();

		},
		onShareAppMessage: function (res) {
			// if (res.from === 'button') {
			//   // 来自页面内转发按钮
			//   console.log(res.target)
			// }
			return {
			  title: this.title,
			  path: '/sm_shop/pages/home/home'
			}
		},
		onShareTimeline:function(){
			return {
			  title: this.title,
			  path: 'sm_shop/pages/home/home'
			}
		},
		onPullDownRefresh() {
		    console.log('refresh');
			var t = this;
		    setTimeout(function () {
				t.init();
		        uni.stopPullDownRefresh();
		    }, 1000);
		},
		components:{
			'component-diy': component_diy,
			trademark
		},
		methods: {

			init: function() {

				var t = this;
				this.home_setting(function( res ){
					
					var settings = res.data.settings;
					t.title = settings.shop_name;
					uni.setNavigationBarTitle({
					                title: t.title 
					            });
					t.diy_id = settings.diy_id;
					if( settings.diy_id ){
						
						t.get_home_diy();
						
					}else{
						// banner 图
						t.get_banner_list();
						
						// 分类模块显示
						t.get_home_category();
						
						//todo 获取首页推荐列表
						t.get_recommend_list();
					}
					
				})
				
			},
			home_setting:function( callback ){
				var url = 'r=home.setting';
				var t = this;				
				app.globalData.request(url, '', 'GET', function(res) {
					
					callback( res );
				
				});
			},
			get_home_diy:function(  ){
				var url = '&r=index.home';
				var t = this;				
				app.globalData.request(url, {m:'sm_shop_plugin_diy'}, 'GET', function(res) {
					t.diy = res.data;
					// console.log( t.diy );
				});
			},
			
			gotoUrl: function( link ) {
				// var link = e.currentTarget.dataset.link;
				uni.navigateTo({
					url: '/sm_shop/pages/web/web?link=' + link,
				});
			},
			get_banner_list: function(resolve) {

				console.log('app:');
				console.log(app);
				// var url = app.api_path + '&r=banner.index&name=home';
				var url = 'r=banner.index&name=home';
				var t = this;

				app.globalData.request(url, '', 'GET', function(res) {

					var bnrHeight = 750 * res.data.img_height / res.data.img_width;
					t.bnrUrl = res.data.image_list;
					t.bnrHeight = bnrHeight;

				});

			},

			get_recommend_list: function(resolve) {

				var t = this;
				/* 				var url = this.api_path+'&r=recommend.index&img_width=280&img_height=380';
								uni.request({
									url		: url,
									success	: res=>{
									    t.recommend = res.data;
										if( typeof resolve == 'function'){
										  resolve( res.data );
										}
									}
								}); */
				var url = 'r=recommend.index&img_width=280&img_height=380';
				app.globalData.request(url, '', 'GET', function(res) {

					t.recommend = res.data;
					if (typeof resolve == 'function') {
						resolve(res.data);
					}

				});

			},

			get_home_category: function() {

				var t = this;
				/* var url = this.api_path + '&r=recommend.home_category';
				uni.request({
					url		: url, 
					success	: res=>{
						
						t.category_show = res.data.status,
						t.category_list = res.data.list
						
					}
				}); */
				var url = 'r=recommend.home_category';
				app.globalData.request(url, '', 'GET', function(res) {

					t.category_show = res.data.status,
						t.category_list = res.data.list;

				});

			},

			gotoBuy: function(product) {
				// var product = e.currentTarget.dataset.product;

				uni.navigateTo({
					// url:'/sm_shop/pages/checkout/checkout?product_id=' + product.id,
					url: '/sm_shop/pages/product/product?product_id=' + product.id,
				});
			},
			
			// 带参跳转到 category tab 页面
			gotoCategoryTab:function( current_cat ){
				
				// this.$store.commit('$uStore',{
				// 	name:'vuex_category_tab_id',
				// 	value:cat_id
				// })
				console.log('category_id:',current_cat);
				this.$u.vuex('vuex_category_tab_id', current_cat.id)
				wx.switchTab({
					url:'/sm_shop/pages/category/category'
				});
			}
		}
	}
</script>
<style lang="less">
	@import './home.less';
</style>
